package org.elasticsearch.aggregate;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.aggregations.InternalAggregation;
import org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class InternalTimeLimiting extends InternalNumericMetricsAggregation.SingleValue {
    private final static Logger logger = LogManager.getLogger(InternalTimeLimiting.class);

    InternalTimeLimiting(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
        super(name, pipelineAggregators, metaData);
    }

    public InternalTimeLimiting(StreamInput in) throws IOException {
        super(in);
        in.readDouble();
    }

    @Override
    protected void doWriteTo(StreamOutput streamOutput) throws IOException {
        streamOutput.writeDouble(0);
    }

    @Override
    public InternalAggregation doReduce(List<InternalAggregation> list, ReduceContext reduceContext) {
        logger.warn("reduce"); // 协调节点执行
        return list.get(0);
    }

    @Override
    public XContentBuilder doXContentBody(XContentBuilder xContentBuilder, Params params) throws IOException {
        xContentBuilder.field(CommonFields.VALUE.getPreferredName(), 0);
        return xContentBuilder;
    }

    @Override
    protected int doHashCode() {
        return 0;
    }

    @Override
    protected boolean doEquals(Object o) {
        return false;
    }

    @Override
    public String getWriteableName() {
        return TimeLimitingAggregationBuilder.NAME;
    }

    @Override
    public boolean isFragment() {
        return false;
    }

    @Override
    public double value() {
        return 0;
    }
}
